﻿using UnityEngine;
using System.Collections;

public class UIUnitCard : MonoBehaviour {
	
	//public string unitName;
	//public string skillName;
	//public string skillDesc;
	//public GameObject unitDescObj;
	public float delayShowDesc = 0.5f;
	public bool isActive = true;
	public Vector3 originalScale;
	public Vector3 originalPos;
	public Vector3 targetPos;

	public Vector3 targetScale = new Vector3(3,3,1);
	public int originalDepth;
	public float newZoomSpeed = 10.0f;
	public float newPosSpeed = 10.0f;
	public bool isScaleUp;
	public bool isDisabled;
	public bool isScaleDownDone = true;
	UIPanel panel;
	UnitCard card;

	public  float timePressed;
	public  bool m_isDown;
	Transform originalParent;
	
	void Awake() {
		
		panel = gameObject.GetComponent<UIPanel>();
		card = gameObject.GetComponent<UnitCard>();
		//SetOriginalPos();
//		if( transform.parent.gameObject.name == "PlayerCardDeck")
//			isActive = true;
//		else
//			isActive = false;
	}

	void Start()
	{

	}

	public void SetOriginalPos()
	{
		float targetPosDiffY = 400;
		//originalPos = transform.localPosition;
		//originalPos = CardManager.Instance.cardPos[card.nPos].localPosition;
		originalPos = transform.localPosition;//CardManager.Instance.GetCardOriginalPos(card);
		targetPos = originalPos;
		if(card.nOrder == 1 || card.nOrder == 4)
			targetPos.y += targetPosDiffY;
		else
			targetPos.y -= targetPosDiffY;
		
		int nOffsetXPos = 0;
		if(card.isBossCard)
			nOffsetXPos = card.nPos;
		
		bool bNoCheckPos = true;
		if(card.isMeCard || card.isBossCard)
			bNoCheckPos = false;
			
		if(bNoCheckPos || card.nPos == nOffsetXPos)
		{
			if(card.nOrder == 1 || card.nOrder == 2)
				targetPos.x += 400;
			else
				targetPos.x -= 400;
		}

		originalScale = gameObject.transform.localScale;
		originalDepth = panel.depth;
		float modScale = 1;
		float scaleFactor=1.2f;
		if(card.isMeCard) {
			scaleFactor=1;
			modScale = CardManager.Instance.cardPos[card.nPos].transform.localScale.x;
		}
		if(gameObject.transform.parent!=null)
			modScale = modScale * gameObject.transform.parent.transform.localScale.x;
		targetScale.x = scaleFactor/modScale;
		targetScale.y = targetScale.x;
		//originalScale = Vector3.one;	//CardManager.Instance.GetCardOriginalScale(card);
		//originalDepth = panel.depth;	//CardManager.Instance.GetCardDepth(card);
		//originalScale = transform.localScale;				
		//originalDepth = panel.depth;		
	}


//	void OnPress (bool isDown)
//	{
//		if(isActive )
//		{
//			base.OnPress(isDown);
//			if(isDown)
//			{
//				
//				SetOriginalPos();
//				ScaleChange(true);
//			
//			}
//			else
//			{
//				ScaleChange(false);
//			}
//		}
//	}
	
	public void ScaleChange(bool isUp)
	{
//		if(BattleManager.Instance.bCombatHappened && BattleManager.Instance.IsBattleJoiner(SID) )
//			return;
		
//		iTween itween = GetComponent<iTween>();
//		if(itween!=null && itween.name != "scaleChange" && itween.isRunning)
//			return;
		
		if(isUp)
		{
			if(!isScaleUp)
			{
				isScaleUp = true;
				
				panel.depth = 10;
				
				//Hashtable ht = iTween.Hash("from",panel.depth, "to", 8 ,"time",0.3f, "onupdate","ChangeDepth");
				//iTween.ValueTo(gameObject,ht);			
				
				//iTween.MoveTo( gameObject, iTween.Hash("name","cardDetailMove", "time", 0.2f, "position", targetPos, "islocal", true,"easetype","easeOutSine" ) );
				//LeanTween.moveLocal( gameObject, targetPos, 0.2f).setEase(LeanTweenType.easeOutSine);
				//iTween.ScaleTo( gameObject, iTween.Hash("name","cardDetailScale", "time", 0.2f, "x", targetScale.x, "y", targetScale.y,  "islocal", true,"easetype","easeOutSine" ) );	
				transform.localPosition = targetPos;
				transform.localScale = targetScale;
			}
		}
		else
		{
			if(isScaleUp)
			{
				isScaleUp = false;
				//panel.depth = originalDepth;
				//isScaleDownDone = false;
				iTween itween = GetComponent<iTween>();
				if(itween!=null && itween.isRunning)
				{
					//iTween.StopByName(gameObject, "cardDetailMove");
					iTween.StopByName(gameObject, "cardDetailScale");
				}
				
				panel.depth = originalDepth;
				gameObject.transform.localPosition = originalPos;
				gameObject.transform.localScale = originalScale;
				
				//Hashtable ht = iTween.Hash("from",panel.depth, "to", originalDepth ,"time",0.3f, "onupdate","ChangeDepth");
				//iTween.ValueTo(gameObject,ht);				
				
				//iTween.MoveTo( gameObject, iTween.Hash("name","scaleChange", "time", 0.3f, "position", originalPos, "islocal", true, "oncomplete","ScaleComplete",  "easetype","easeOutSine" ) );
				//iTween.ScaleTo( gameObject, iTween.Hash("name","scaleChange", "time", 0.3f, "x", originalScale.x, "y", originalScale.y,  "islocal", true,"easetype","easeOutSine" ) );
			}			
		}
	}
	
//	void ScaleComplete()
//	{
//		glog.debug("scale complete");
//		isScaleDownDone = true;
//	}
	
//	void ChangeDepth(float newValue)
//	{
//		//glog.debug("change depth "+newValue);
//		if((int)newValue == originalDepth)
//		{
//			panel.depth = (int)newValue;
//		}
//	}
	
//	public void SetEnable(bool bEnable)
//	{
//		isActive = bEnable;
//		if(!bEnable)
//		{
//			if(isScaleUp || panel.depth != originalDepth)
//			{
//				isScaleUp = false;
//				panel.depth = originalDepth;
//				gameObject.transform.localPosition = originalPos;
//				gameObject.transform.localScale = originalScale;
//			}
//		}
//	}
	
	public void SetDisableAndRestore(float delay)
	{
		if(isActive)
		{
			if(gameObject.activeSelf)
				StartCoroutine( CoSetDisableAndRestore(delay) );
		}
	}
	
	IEnumerator CoSetDisableAndRestore(float delay)
	{
		ScaleChange(false);
		yield return new WaitForSeconds(delay+0.1f);
		if(!isDisabled)
			isActive = true;
	}
	
	// Update is called once per frame
	void Update () {

		/*
		if(isActive )
		{
			if(timePressed>0.1f)
			{
				//unitDescObj.SetActive(true);
				//glog.debug("show card detail");
//				panel.depth = 8;
				//targetPos = originalPos + new Vector3(0,targetPosDiffY,0);
//				transform.localScale = Vector3.Lerp(transform.localScale, targetScale, zoomSpeed*Time.deltaTime);
//				transform.localPosition = Vector3.Lerp(transform.localPosition, targetPos, posSpeed*Time.deltaTime);
				
			}
			else 
			{
				//unitDescObj.SetActive(false);
//				panel.depth = originalDepth;
	//			transform.localScale = Vector3.Lerp(transform.localScale, originalScale, zoomSpeed*Time.deltaTime);
	//			transform.localPosition = Vector3.Lerp(transform.localPosition, originalPos, posSpeed*Time.deltaTime);
			}
			
		}
		else
		{
			timePressed = 0.0f;
		}
		*/

		if(m_isDown)
		{
			timePressed += Time.deltaTime;
			if(timePressed>0.1f)
			{
				card.cardState = ECardState.TouchZoom;
				panel.depth = 45;
				transform.parent = CardManager.Instance.centerAnchor.transform;
				transform.localScale = Vector3.Lerp(transform.localScale, targetScale, newZoomSpeed*Time.deltaTime);
				transform.localPosition = Vector3.Lerp(transform.localPosition, targetPos, newPosSpeed*Time.deltaTime);
			}
		}
		else
		{
			timePressed = 0.0f;
		}
	}

	void OnPress (bool isDown)
	{
		if(card.cardState== ECardState.Still || card.cardState== ECardState.TouchZoom)
		{
			m_isDown = isDown;
			if(m_isDown)
			{
				originalScale = transform.localScale;
				originalPos = transform.localPosition;
				originalDepth = panel.depth;
				originalParent = transform.parent;
				targetScale = Vector3.one;
				targetPos = Vector3.zero;
			}
			else
			{
				StopTouchZoom();
			}
		}
	}

	public void StopTouchZoom()
	{
		m_isDown=false;
		timePressed=0.0f;
		if(card.cardState== ECardState.TouchZoom)
		{
			transform.parent = originalParent;
			transform.localPosition = originalPos;
			transform.localScale = originalScale;
			panel.depth = originalDepth;
		}
		card.cardState= ECardState.Still;
	}
}
